package com.rssreader.client.components;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.tips.ToolTipConfig;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rssreader.client.RSSReaderConstants;
import com.rssreader.client.lists.FeedList;
import com.rssreader.client.services.FeedServiceAsync;
import com.rssreader.client.windows.FeedWindow;
import com.rssreader.shared.model.Feed;

public class CopyOfRssNavigationPanel extends ContentPanel {
	public CopyOfRssNavigationPanel(){
		setHeading("Navigation");
		final ToggleButton btnLinkFeed = new ToggleButton("Link feed");
		btnLinkFeed.setIconStyle("link-feed");
		setButtonAlign(HorizontalAlignment.LEFT); //btnLinkFeed在RssNavigationPanel容器里水平居左
		
		
		ToolTipConfig linkFeedToolTipConfig = new ToolTipConfig();
		linkFeedToolTipConfig.setTitle("Link to existing RSS feed");
		linkFeedToolTipConfig.setText("Allows you to enter the URL " +
				"of an existing RSS feed you would like to link to");
		btnLinkFeed.setToolTip(linkFeedToolTipConfig);
		
		final LinkFeedPopup linkFeedPopup = new LinkFeedPopup();
		linkFeedPopup.setConstrainViewport(true); //要保證pop up的彈出始終在整個面板Viewport內
		
		btnLinkFeed.addSelectionListener(new SelectionListener<ButtonEvent>(){
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				if(btnLinkFeed.isPressed()){
					linkFeedPopup.show(btnLinkFeed.getElement(), "bl-tl?");
				}else{
					linkFeedPopup.hide();
				}
			}
		});
		
		final Button btnCreateFeed = new Button("Create feed");
		btnCreateFeed.setIconStyle("create-feed");
		
		ToolTipConfig createNewToolTipConfig = new ToolTipConfig();
		createNewToolTipConfig.setTitle("Create a new RSS feed");
		createNewToolTipConfig.setText("Creates a new RSS feed");
		btnCreateFeed.setToolTip(createNewToolTipConfig);
		
		btnCreateFeed.addSelectionListener(new SelectionListener<ButtonEvent>(){
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				createNewFeedWindow();
			}
		});
		
		addButton(btnCreateFeed);
		addButton(btnLinkFeed);
		//set the layout to a new instance of FitLayout
		setLayout(new FitLayout());
		//add a new instance of the FeedList component to the underlying ContentPanel
		add(new FeedList());
	}
	
	private void createNewFeedWindow()
	  {
	    /*final Window newFeedWindow = new FeedWindow(new Feed());
	    newFeedWindow.show();*/
		
		/*
			通過GWT的RPC去調用server端了（FeedServiceImpl）
			首先調用FeedServiceImpl.createNewFeed()方法。
			當執行完畢之後，成功了就會回調onSuccess()，
			反之失敗了，就會回調onFailure()方法
		*/
		final FeedServiceAsync feedService = Registry.get(RSSReaderConstants.FEED_SERVICE);
		feedService.createNewFeed(new AsyncCallback<Feed>() {
			
			@Override
			public void onSuccess(Feed feed) {
				final Window newFeedWindow = new FeedWindow(feed);
				newFeedWindow.show();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Info.display("RSSReader", "Unable to create a new feed");
			}
		});
	  }
}
